package com.xian.gaoxiao.db;


import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.util.Log;

public class CommentTable implements AppDBConstants {

	public static final String TAG = "CommentTable";
	public static final String TABLE_NAME = Tables.TASKCOMMENT;

	private SQLiteDatabase db;

	public CommentTable(SQLiteDatabase db) {
		this.db = db;
	}

	public static class CommentColumns implements BaseColumns {
		public static final String ID = "_id";
		public static final String TASKID = "taskid";
		public static final String CREATEDATE = "createdate"; // auto
		public static final String MODIFYDATE = "modifydate";
		public static final String ISSHOW = "isshow"; // default 0 ， 0显示 1不显示
		public static final String COMMENT = "comment";
		public static final String SORT = "sort"; // default 0
		public static final String PIC = "pic";
	}

	public int insert(Integer taskid, String comment, byte[] pic) {
		int result = 0;
		try {
			StringBuilder builder = new StringBuilder();
			builder.append("INSERT INTO ");
			builder.append(TABLE_NAME);
			builder.append(" (");
			builder.append(CommentColumns.TASKID);
			builder.append(" ,");
			builder.append(CommentColumns.CREATEDATE);
			builder.append(" ,");
			builder.append(CommentColumns.COMMENT);
			builder.append(" ,");
			builder.append(CommentColumns.PIC);
			builder.append(" ) VALUES (");
			builder.append(taskid);
			builder.append(",");
			builder.append(System.currentTimeMillis());
			builder.append(",'");
			builder.append(comment);
			builder.append("',");
			builder.append(pic);
			builder.append(" )");
			Log.e(TAG, builder.toString());
			db.execSQL(builder.toString());
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}


	public int delete(String where,String[] args) {
		int result = 0;
		try {
			result = db.delete(TABLE_NAME, where, args);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public int update(ContentValues values, String whereClause,
			String[] whereArgs) {
		int result = 0;
		try {
			values.put(CommentColumns.MODIFYDATE, System.currentTimeMillis());
			result = db.update(TABLE_NAME, values, whereClause, whereArgs);
			return result;
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}

	}

	public Cursor query(String[] columns, String selection,
			String[] selectionArgs, String orderBy, String limit) {
		Cursor cursor = null;
		try {
			cursor = db.query(TABLE_NAME, columns, selection, selectionArgs,
					null, null, orderBy, limit);
			return cursor;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

}
